<?php
/**
 * Created by PhpStorm.
 * User: 10838
 * Date: 2018/2/10
 * Time: 10:11
 */
namespace app\admin\controller;

use mg_api\Mg_api;
use \think\Controller;
use think\Db;

class Same extends Controller{

    public function update(){

       $data =  Db::table('dx_same')->find(1);

        $this->assign('data',$data);
          return view('/same/edit');
    }
    public function update_from(){
       $tel = $this->request->post('tel')??'';
        $openid = $this->request->post('openid')??'';

        if(!$tel || !$openid) {
            $this->error('手机号与openid不能为空');
        }
        $mg_api = new Mg_api();
        $data = [
            'key' => 'isTrue',
            'F_Tel' => (int)$tel,

        ];
        $tel_data = $mg_api->api($data);
        $tel_data = json_decode($tel_data, true);
        if($tel_data['Message'] != '该电话号码存在。'){
            $this->error('没有查询到此电话');
        }
        $data_openid = [
            'key'=>'Get_LinkMan',
            'wx_openid'=>$openid
        ];
        $re_openid = $mg_api->api($data_openid);
        $re_openid = json_decode($re_openid, true);

        if(!$re_openid['Status']){
            $this->error('没有获取到该openid的数据');
        }
        $tels = explode('/', $re_openid['JData']['0']['f_tel']);
        if(!in_array($tel,$tels)){
            $this->error('该手机与此微信openid不匹配');
        }
        $re = Db::table('dx_same')->where('id',1)->update([
            'tel'=>$tel,
            'openid'=>$openid
        ]);
        if($re){
            $this->success('修改成功',Url('admin/same/update'));
        }else{
            $this->error('修改失败');
        }





    }



}